/**
 * Created by Administrator on 2015/7/14.
 */

//删除店铺 删除自己
function deleteShop(obj){
    var flag = false;
    var i = 0;
    obj.parents('ul').find('.changeSelected').each(function (e) {
        if($(this).hasClass('active')){
            flag = true;
        }
        i++;
    })
    if(flag){
        changeSelected();
    }else{
        //删除自己
        if(i == 1){
            var j = 0;
            obj.parents('ul').find('.checkShop').each(function (e) {
                j++;
            })
            //最后一个店铺 删除从新刷页面
            if(j <= 1){
                window.location.reload();
            }else{
                //删除店铺
                obj.parents('.goods-shop').remove();
            }

        }else{
            //不止一个子产品 删除自己
            obj.parents('li').remove();

        }
    }
}

//刷新购物车
function changeSelected(){
    var id = [];
    var uid = [];
    $('.changeSelected').each(function (e) {
        if($(this).hasClass('active')){
            id.push($(this).attr('data-id'));
        }
    });

    $('.checkShop').each(function (e) {
        if($(this).hasClass('active')){
            uid.push($(this).attr('data-uid'));
        }
    });
    if(id.length > 0){
        $.ajax({
            type:'post',
            async:false,
            url:hostUrl()+'tourismcart/changeSelected',
            data:{'uid':uid,'id':id},
            dataType:'json',
            beforeSend : function (evt, request, settings) {
                loading();
            },
            success:function(json){
                if(json.status){
                    $('.shopping-product').html(json.html);
                    $('#cart-totalPrice').html(json.total);
                    $('#selectedItemsCount').html(json.num);
                    $('.am-btn-danger').removeAttr('disabled');
                    checkAll();//全选
                    if(json.num > 0){
                        $('.am-btn-danger').addClass('active');
                    }
                }else{

                }
                closeLoading();
            }
        })
    }else{
        cleanCheckAll();
    }
}

//判断是否选择
function checkItemSelected(obj){
    $item = obj.parents('ul').children('.shop-header').find('.checkShop');
    if($item.hasClass('active')){
        changeSelected();
    }
};

//清除全选 数量
function cleanCheckAll(){
    $('#cart-totalPrice').html(0);
    $('#selectedItemsCount').html(0);
    $('.am-btn-danger').removeClass('active');
    $('.am-btn-danger').attr('disabled', 'disabled');
    $('.checkAll').removeClass('active');
};

//判断是否全选
function checkAll(){
    var flag = true;
    $('.checkShop').each(function (e) {
        if(!$(this).hasClass('active')){
            flag = false;
        }
    })
    if(flag){
        $('.checkAll').addClass('active');
    } else {
        $('.checkAll').removeClass('active');
    }
};

/****
 * 清空失效的商品
 */
function clearCartDisabled(){
    $.ajax({
        type:'post',
        async:false,
        url:hostUrl()+'tourismcart/clearCartDisabled',
        data:'',
        dataType:'json',
        beforeSend : function (evt, request, settings) {
            loading();
        },
        success:function(json){
            closeLoading();
            if(json.status){
                //$('ul.shop-shxiao').remove();
                changeSelected();
            }else{
                //alertMessage(json.messages);
                window.location.reload();
            }
        }
    })
};

$(function(){
    /*******************************************购物车***************************************/
        //全选
    $('.am-last').on(status, '.checkAll', function(){
        if($(this).hasClass('active')){
            $('.changeSelected').removeClass('active');
            $('.checkShop').removeClass('active');
            $(this).removeClass('active');
            cleanCheckAll();
        } else {
            $('.changeSelected').addClass('active');
            $('.checkShop').addClass('active');
            $(this).addClass('active');
            $('.am-btn-danger').attr('disabled', false);
            changeSelected();
        }
    });

    //选择商店
    $('.shopping-product').on(status, '.checkShop', function(){
        if($(this).hasClass('active')){
            $(this).parents('.shop-header').next('ul').find('.changeSelected').removeClass('active');
            $(this).removeClass('active');
        } else {
            $(this).parents('.shop-header').next('ul').find('.changeSelected').addClass('active');
            $(this).addClass('active');
        }

        changeSelected();
    });

    //单个商品选择
    $('.shopping-product').on(status, '.changeSelected', function(){
        if($(this).hasClass('active')){
            $(this).removeClass('active');
            $(this).parents('ul').prev('.shop-header').find('.checkShop').removeClass('active');
        } else {
            var flag = true;
            $(this).addClass('active');

            $(this).parents('ul').children('li').find('.changeSelected').each(function (e) {
                if(!$(this).hasClass('active')){
                    flag = false;
                }
            })
            if(flag){
                $(this).parents('ul').prev('.shop-header').find('.checkShop').addClass('active');
            }else{
                $(this).parents('ul').prev('.shop-header').find('.checkShop').removeClass('active');
            }
        }
        changeSelected();
    });

    //删除商品 购物车
    $('.shopping-product').on(status,'.del',function(){
        var id = $(this).attr('data-id');
        var obj = $(this);
        var nconfirm = layer.open({
            type: 1, //1代表页面层
            content: '<div class="content"><div class="title">确定要删除该商品吗？</div></div><div class="con-body am-cf"><a class="am-btn closediy">取消</a><a class="am-btn closediy">确定</a></div>',
            style: 'max-width:14.29em;padding:0.714em;border-radius:0.25em',
            shadeClose:false,
            success: function(olayer){
                var cla = 'getElementsByClassName';
                olayer[cla]('closediy')[0].onclick = function(){
                    layer.close(nconfirm)
                }
                olayer[cla]('closediy')[1].onclick = function(){
                    $.ajax({
                        type:'post',
                        async:false,
                        url:hostUrl()+'tourismcart/cartDelete',
                        data:{'id':id},
                        dataType:'json',
                        success:function(json){
                            if(json.status){
                                deleteShop(obj);
                            }else {
                                alertMessage(json.messages);
                            }
                        }
                    })
                    deleteShop(obj);
                    layer.close(nconfirm);
                }
            }
        });
    });

    //提交
    $('.am-last').on(status, '.am-btn-danger', function(){
        var id = '';
        $(this).attr('disabled', 'disabled');
        $(this).html('正在提交...');
        $('.changeSelected').each(function (e) {
            if($(this).hasClass('active')){
                id += $(this).attr('data-id')+',';
            }
        })
        if(id.length > 0){
            $.ajax({
                type:'post',
                async:false,
                url:hostUrl()+'order/setCartCookie',
                data:{'id':id,'cbk':1},
                dataType:'json',
                beforeSend : function (evt, request, settings) {
                    loading();
                },
                success:function(json){
                    closeLoading();
                    if(json.status){
                        window.location.href = hostUrl()+'order/confirm';
                    }else{
                        alertMessage(json.messages);
                    }
                }
            })
        }
    });
});